package cn.fl.mcr.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.export.McrBillCheckDetailExportBean;
import cn.fl.mcr.export.McrBillCheckExportBean;
import cn.fl.mcr.service.IMcrBillCheckService;
import cn.fl.mcr.transform.BaseMcrBillCheckDetailVOTransform;
import cn.fl.mcr.transform.BaseMcrBillCheckVOTransform;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.constant.McrLoanConstant;
import cn.lionbridgecapital.loan.model.dto.McrBillCheckDTO;
import cn.lionbridgecapital.loan.model.utils.Safes;
import cn.lionbridgecapital.loan.model.vo.McrBillCheckVO;
import cn.lionbridgecapital.loan.service.api.IMcrBillCheckApiService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 对账
 *
 * @Author: shaozhou.li
 * @Date: 2020/4/17 21:39
 */
@Service
public class McrBillCheckServiceImpl implements IMcrBillCheckService {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(McrBillCheckServiceImpl.class);

    @Resource
    private IMcrBillCheckApiService mcrBillCheckApiService;

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if(null == page) {
            return new Pager();
        }

        page = this.mcrBillCheckApiService.searchMcrBillCheckListPage(page);
        List<McrBillCheckVO> voList=  Safes.of((List<McrBillCheckDTO>)page.getResultList()).stream()
                .map(it -> BaseMcrBillCheckVOTransform.baseToVO(it))
                .collect(Collectors.toList());
        page.setResultList(voList);
        return page;
    }

    @Override
    public List<McrBillCheckExportBean> exportDiffDetail(Long checkId) throws BaseException {
        return Safes.of(mcrBillCheckApiService.selectCheckDetailList(checkId)).stream()
                .filter(it -> StringUtils.equals(it.getCheckStatus(), McrLoanConstant.BillFileDataStatus.DIFFERENCE_YES))
                .map(it -> BaseMcrBillCheckDetailVOTransform.dtoToExport(it))
                .collect(Collectors.toList());
    }

    @Override
    public McrBillCheckVO searchMcrBillCheck(Long id) {
        return BaseMcrBillCheckVOTransform.baseToVO(mcrBillCheckApiService.selectMcrBillCheckById(id));
    }

    @Override
    public List<String> readLoanFile(Long checkId) throws BaseException {
        return mcrBillCheckApiService.readLoanFile(checkId);
    }

    @Override
    public List<McrBillCheckDetailExportBean> searchMcrBillCheckDetail(Long checkId) throws BaseException {
        return Safes.of(mcrBillCheckApiService.searchMcrBillCheckDetail(checkId)).stream()
                .map(it -> BaseMcrBillCheckDetailVOTransform.checkDetailToExport(it))
                .collect(Collectors.toList());
    }

    @Override
    public boolean modify(McrBillCheckVO entity) throws BaseException {
        if(null == entity){
            BaseLogUtils.info(logger, "modify", "修改对账对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
            return false;
        }
        return this.mcrBillCheckApiService.modifyMcrBillCheckDTO(BaseMcrBillCheckVOTransform.baseToDTO(entity));

    }
}
